//
//  AddAnswerView.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/29.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "AddAnswerView.h"

@interface AddAnswerView ()

@property (nonatomic, strong) UIView * whiteBackView; // 白色背景
@property (nonatomic, strong) UITextView * inputTextView; // 信息录入
@property (nonatomic, strong) UIButton * commitButton; // 提交按钮
@property (nonatomic, strong) UIButton * cancelButton; // 取消按钮

@end

@implementation AddAnswerView

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithSendMessageBlock:(SendMessageBlock)sendmessageBlock
{
    self = [super init];
    if (self) {
        self.backgroundColor = RGB_COLOR(255, 255, 255, 0.35);
        
        // 点击发送回调
        _sendMessageBlockl = sendmessageBlock;
        // 添加视图
        [self addSubview:self.whiteBackView];
        [_whiteBackView addSubview:self.inputTextView];
        [_whiteBackView addSubview:self.commitButton];
        [_whiteBackView addSubview:self.cancelButton];
        
        // 自动布局
        [_whiteBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self).multipliedBy(0.48);
            make.height.equalTo(_whiteBackView.mas_width).multipliedBy(0.65);
            make.centerX.equalTo(self).offset(SCREEN_WIDTH * 0.195 * 0.5);
            make.centerY.equalTo(self);
        }];
        [_inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(_whiteBackView).multipliedBy(0.9);
            make.height.equalTo(_whiteBackView).multipliedBy(0.7);
            make.centerX.equalTo(_whiteBackView);
            make.centerY.equalTo(_whiteBackView).multipliedBy(0.83);
        }];
        [_commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(_whiteBackView).multipliedBy(0.23);
            make.height.equalTo(_commitButton.mas_width).multipliedBy(0.35);
            make.centerX.equalTo(_whiteBackView).multipliedBy(0.7);
            make.centerY.equalTo(_whiteBackView).multipliedBy(1.8);
        }];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(_commitButton);
            make.centerX.equalTo(_whiteBackView).multipliedBy(1.3);
            make.centerY.equalTo(_commitButton);
        }];
    }
    return self;
}

#pragma mark 🌷🌷🌷event responds🌷🌷🌷
- (void)respondsToCommitButton {
    if (_inputTextView.text.length == 0) {
        [MBProgressHUD showError:@"发送内容不能为空！"];
        return;
    }
    _sendMessageBlockl(_inputTextView.text);
    [self removeFromSuperview];
}
- (void)respondsToCancelButton {
    // 移除本视图
    [self removeFromSuperview];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UIView *)whiteBackView {
    if (!_whiteBackView) {
        _whiteBackView = [[UIView alloc] init];
        _whiteBackView.backgroundColor = [UIColor whiteColor];
        _whiteBackView.layer.borderColor = RGB_COLOR(184, 184, 184, 1).CGColor;
        _whiteBackView.layer.borderWidth = 1;
    }
    return _whiteBackView;
}
- (UITextView *)inputTextView {
    if (!_inputTextView) {
        _inputTextView = [[UITextView alloc] init];
        _inputTextView.backgroundColor = RGB_COLOR(239, 239, 239, 1);
        _inputTextView.layer.borderColor = RGB_COLOR(186, 186, 186, 1).CGColor;
        _inputTextView.layer.borderWidth = 1;
        _inputTextView.font = [UIFont systemFontOfSize:20];
        _inputTextView.textColor = RGB_COLOR(189, 189, 189, 1);
    }
    return _inputTextView;
}
- (UIButton *)commitButton {
    if (!_commitButton) {
        _commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _commitButton.layer.borderColor = RGB_COLOR(207, 207, 207, 1).CGColor;
        _commitButton.layer.borderWidth = 2;
        _commitButton.layer.cornerRadius = 10;
        _commitButton.titleLabel.font = [UIFont boldSystemFontOfSize:23];
        [_commitButton setTitle:@"提交" forState:UIControlStateNormal];
        [_commitButton setTitleColor:RGB_COLOR(36, 180, 192, 1) forState:UIControlStateNormal];
        [_commitButton addTarget:self action:@selector(respondsToCommitButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commitButton;
}
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.layer.borderColor = RGB_COLOR(207, 207, 207, 1).CGColor;
        _cancelButton.layer.borderWidth = 2;
        _cancelButton.layer.cornerRadius = 10;
        _cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:23];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:RGB_COLOR(36, 180, 192, 1) forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(respondsToCancelButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

@end
